import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Select } from 'antd';
let timeout;
let currentValue;
let currentSubject;
const Option = Select.Option;
@connect(({ administration3, loading }) => ({
  administration:administration3,
  loading: loading.models.administration3,
}))
class TeacherCalendarSearchInput extends PureComponent {
  state = {
    data: [],
    value: '',
  };
  componentDidMount() {
    this.setState({
      value: this.props.teacherName || '',
    });
  }
  fetch = (value, subject, callback) => {
    const { dispatch } = this.props;
    let _this = this;
    if (timeout) {
      clearTimeout(timeout);
      timeout = null;
    }
    currentValue = value;
    currentSubject = subject;
    function fake() {
      //发送请求
      dispatch({
        type: 'administration3/getSimilarLessonTeacher',
        payload: 'subject=' + currentSubject + '&teacherName=' + currentValue,
      }).then(() => {
        const { similarTeacherList } = _this.props.administration;
        if (similarTeacherList) {
          callback(similarTeacherList);
        }
      });
    }

    timeout = setTimeout(fake, 300);
  };
  handleSearch = value => {
    this.fetch(value, this.props.subject, data => this.setState({ data }));
  };

  handleChange = value => {
    this.setState({ value });
    var selectObj = this.state.data.find(item => item.id == value);
    this.props.onOk(selectObj);
  };

  render() {
    const options = this.state.data.map((d, index) => (
      <Option key={index} value={d.id}>
        {d.name}
      </Option>
    ));
    return (
      <Select
        showSearch
        style={{ width: this.props.myStyle }}
        value={this.state.value}
        placeholder="请输入老师姓名"
        defaultActiveFirstOption={false}
        showArrow={false}
        filterOption={false}
        onSearch={this.handleSearch}
        onChange={this.handleChange}
        notFoundContent={null}
      >
        {options}
      </Select>
    );
  }
}

export default TeacherCalendarSearchInput;
